package com.ext.aic.handler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.fragment.app.FragmentActivity;

import com.ext.aic.AICApplication;
import com.ext.aic.ui.AISeparateSettingActivity;
import com.ext.aic.ui.MainActivity;
import com.ext.aic.utils.BundleUtils;

import java.util.ArrayList;
import java.util.List;

//Activity回调兼容处理
public abstract class ActivityResultHandler {

    private boolean mIsResultHandled = false;
    private boolean mIsStartActivityResult = false;
    private List<Integer> switchEnterSequence = new ArrayList<>();
    private Bundle extras;
    private int requestCode;
    private ActivityResultLauncher<Intent> accessibilityLauncher;

    //最终回调结果
    protected abstract void onFinallyResult(Bundle bundle);

    //在对应的Activity中的onCreate方法中调用此方法
    public void onCreate(FragmentActivity activity) {
        mIsResultHandled = false;
        mIsStartActivityResult = true;
        accessibilityLauncher = activity.registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        mIsResultHandled = true;
                        Intent data = result.getData();
                        forResultHandler(data);
                        onFinallyResult(extras);
                    }
                }
        );
    }

    private void forResultHandler(Intent data) {
        if (data == null) {
            return;
        }
        Bundle bundle = data.getExtras();
        //这里合并bundle数据
        if (extras == null) {
            extras = new Bundle();
        }
        BundleUtils.mergeBundle(bundle, extras);
    }

    //在对应的Activity中的onResume方法中调用此方法
    public void onResume() {
        if (mIsStartActivityResult || mIsResultHandled) {
            mIsStartActivityResult = false;
            return;
        }
        switchEnterSequence.add(1);
        if (extras == null) {
            extras = new Bundle();
        }
        if (switchEnterSequence.size() >= 2) {
            Integer first = switchEnterSequence.get(0);
            Integer second = switchEnterSequence.get(1);
            if (first == 2 && second == 1 &&
                    (AICApplication.isActivityTop(MainActivity.class) ||
                            AICApplication.isActivityTop(AISeparateSettingActivity.class))) {
                onFinallyResult(extras);
                switchEnterSequence.clear();
            }
        }
    }

    //在对应的Activity中的onPause方法中调用此方法
    public void onPause() {
        switchEnterSequence.add(2);
    }

    //在对应的Activity中的onActivityResult方法中调用此方法
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
            mIsResultHandled = true;
            forResultHandler(data);
            onFinallyResult(extras);
        }
    }

    //启动带有回调的Activity
    public void startActivityForResult(FragmentActivity fragmentActivity, Intent intent, int requestCode) {
        this.requestCode = requestCode;
        try {
            extras = intent.getExtras();
            if (extras == null) {
                extras = new Bundle();
            }
            extras.putInt("requestCode", requestCode);
            accessibilityLauncher.launch(intent);
        } catch (Exception e) {
            fragmentActivity.startActivityForResult(intent, requestCode);
        }
    }
}
